package com.xiaoge.system.controller;

import com.xiaoge.response.Result;
import com.xiaoge.response.ResultCode;
import com.xiaoge.system.service.LoginService;
import com.xiaoge.system.utils.redisUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@Api("登录页面")
@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {

    @Autowired
    public LoginService loginService;

    @Autowired
    public redisUtils redisUtils;

    /**
     * 验证用户登录方法
     */
    @PostMapping("/checkLogin")
    public Result checkUserNameIsCurrent(@RequestParam String userName,
                                         @RequestParam String password,
                                         HttpServletResponse res,
                                         HttpServletRequest request
                                         ) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        String b = loginService.queryIsHaveUser(userName, password);
        request.getSession().setAttribute("userid",b);
        request.getSession().setAttribute("username",userName);
        if (b !=null && !"".equals(b)){
            redisUtils.incr("traffic",1L);

            //System.out.println(redisUtils.get("traffic"));
            return Result.ok().data("code","Y");
        }else{
            return Result.ok().data("code","N");
        }
    }
}
